/*
 * 좌표계
 * 
 * ^ z
 * |     ㄱ y
 * |    /
 * |   /
 * |  /
 * | /
 *   -----------> x 
 */

public class TestMain {

	public static void main(String[] args) {
		GObject block1 = new GObject();
		GObject block2 = new GObject();

		block1.setPosition(new Point3D(0, 0, 0, "m"));
		block2.setPosition(new Point3D(10, 10, 0, "m"));

		block1.setSize(new Point3D(10, 10, 0, "m"));
		block2.setSize(new Point3D(10, 10, 0, "m"));

		System.out.println("거리 : "
				+ getDistanceBetweenObjectsCenters(block1, block2, "m"));
		System.out.println(collisionCheck2DPad(block1, block2, 90));
	}

	/**
	 * GObject 의 외곽 사각 블럭을 이용하여 충돌 여부를 확인한다.
	 * 
	 * @param go1
	 *            충돌 확인 할 GObject1
	 * @param go2
	 *            충돌 확인 할 GObject2
	 * @return 충돌여부
	 */
	public static boolean collisionCheck2D(GObject go1, GObject go2) {
		double go1Left = go1.getPosition().getX();
		double go1Right = go1.getPosition().getX() + go1.getSize().getX();
		double go1Top = go1.getPosition().getY() + go1.getSize().getY();
		double go1Bottom = go1.getPosition().getY();

		double go2Left = go2.getPosition().getX();
		double go2Right = go2.getPosition().getX() + go2.getSize().getX();
		double go2Top = go2.getPosition().getY() + go2.getSize().getY();
		double go2Bottom = go2.getPosition().getY();

		if (go1Right > go2Left && go1Left < go2Right && go1Top > go2Bottom
				&& go1Bottom < go2Top)
			return true;

		return false;
	}

	/**
	 * GObject 의 외곽 사각 블럭에 패드를 적용하여 충돌 여부를 확인한다.
	 * 
	 * @param go1
	 *            충돌 확인 할 GObject1
	 * @param go2
	 *            충돌 확인 할 GObject2
	 * @param percent
	 *            GObject의 외곽 패드율.
	 * @return 충돌여부
	 */
	public static boolean collisionCheck2DPad(GObject go1, GObject go2,
			int percent) {
		double rate = (100 - percent) * 0.01;
		double go1Width = go1.getSize().getX();
		double go1Height = go1.getSize().getY();
		double go2Width = go2.getSize().getX();
		double go2Height = go2.getSize().getY();

		double go1Left = go1.getPosition().getX() + go1Width * rate;
		double go1Right = go1.getPosition().getX() + go1.getSize().getX()
				- go1Width * rate;
		double go1Top = go1.getPosition().getY() + go1.getSize().getY()
				- go1Height * rate;
		double go1Bottom = go1.getPosition().getY() + go1Height * rate;

		double go2Left = go2.getPosition().getX() + go2Width * rate;
		double go2Right = go2.getPosition().getX() + go2.getSize().getX()
				- go2Width * rate;
		double go2Top = go2.getPosition().getY() + go2.getSize().getY()
				- go2Height * rate;
		double go2Bottom = go2.getPosition().getY() + go2Height * rate;

		if (go1Right > go2Left && go1Left < go2Right && go1Top > go2Bottom
				&& go1Bottom < go2Top)
			return true;

		return false;
	}

	/**
	 * GObject 간의 거리를 잰다. 좌측 하단 기준.
	 * 
	 * @param obj1
	 *            GObject1
	 * @param obj2
	 *            GObject2
	 * @param measure
	 *            단위("mm" : 밀리미터, "cm" : 센티미터, "m" : 미터, "km" : 킬로미터)
	 * @return 단위로 환산된 GObject 간의 거리
	 */
	public static double getDistanceBetweenObjects(GObject obj1, GObject obj2,
			String measure) {
		GObject distanceGObject = new GObject();
		Point3D distancePoint3D = Point3D.subPoint3D(obj1.getPosition(), obj2
				.getPosition());
		distanceGObject.setSize(distancePoint3D);
		return distanceGObject.getDiameter(measure);
	}

	/**
	 * GObject 간의 거리를 잰다. GObject 중심 기준.
	 * 
	 * @param obj1
	 *            GObject1
	 * @param obj2
	 *            GObject2
	 * @param measure
	 *            단위("mm" : 밀리미터, "cm" : 센티미터, "m" : 미터, "km" : 킬로미터)
	 * @return 단위로 환산된 GObject 간의 거리
	 */
	public static double getDistanceBetweenObjectsCenters(GObject obj1,
			GObject obj2, String measure) {
		Point3D obj1Center = obj1.getObjectCenter();
		Point3D obj2Center = obj2.getObjectCenter();

		GObject distanceGObject = new GObject();
		Point3D distancePoint3D = Point3D.subPoint3D(obj1Center, obj2Center);
		distanceGObject.setSize(distancePoint3D);
		return distanceGObject.getDiameter(measure);
	}
}
